<?php
/**
 * Created by PhpStorm.
 * User: xieky
 * Date: 2017/8/7
 * Time: 17:39
 */

namespace common\services\myhtsq;

use common\entities\ConfigEntity;
use common\entities\myhtsq\ApplicationBuRateEntity;
use common\repositories\myhtsq\ApplicationInfoRepository;
use common\repositories\myhtsq\ErpBusinessUnitRepository;
use common\services\BaseService;
use common\entities\myhtsq\CarryoverTimeEntity;
use common\repositories\myhtsq\ContractRevenueRepository;
use common\entities\myhtsq\ApplicationBalanceRateEntity;
use common\support\StringHelper;

class ParamsService extends BaseService
{

    public static function getParamsCarryoverTime(){
        return CarryoverTimeEntity::find()->all();
    }

    public static function saveParamsCarryoverTime($data){
        $ct = CarryoverTimeEntity::find()->one();
        $ct->carryover_begin_time = $data['carryover_begin_time'];
        if(strlen($data['carryover_end_time']) == 10){
            $ct->carryover_end_time = $data['carryover_end_time'] . ' 23:59:59';
        }else {
            $ct->carryover_end_time = str_replace("00:00:00", "23:59:59", $data['carryover_end_time']);
        }
        return $ct->save();
    }

    public static function checkCanSave($params){
        $oid = empty($params['oid'])?'':$params['oid'];
        $appid = $params['appid'];
        $buid = $params['buid'];
        if(empty($oid)) {
            $row = ApplicationBuRateEntity::find()->where(['balance_rate_id' => $appid, 'BUGUID' => $buid])->one();
        }else{
            $row = ApplicationBuRateEntity::find()->where("balance_rate_id='".$appid."' AND BUGUID='".$buid."' AND application_bu_rate_id<>'".$oid."'")->one();
        }
        if(empty($row)){
            return true;
        }else{
            return false;
        }
    }

    public static function saveBalanceRate($params){
        $oid = empty($params['oid'])?'':$params['oid'];
        $appid = $params['appid'];
        $buid = $params['buid'];
        $rate = $params['rate'];
        if(empty($oid)){
            $raterow = new ApplicationBuRateEntity();
            $raterow->application_bu_rate_id = StringHelper::uuid();
            $raterow->created_on = ConfigEntity::getDbNowDate();
        }else{
            $raterow = ApplicationBuRateEntity::find()->where(['application_bu_rate_id'=>$oid])->one();
            $raterow->modified_on = ConfigEntity::getDbNowDate();
        }
        $raterow->balance_rate_id = $appid;
        $raterow->BUGUID = $buid;
        $raterow->rate = $rate;
        if($raterow->save()){
            return true;
        }else{
            return false;
        }
    }

    public static function getCanChangeCarryoverTime(){
        $revlists = ContractRevenueRepository::getCarryoverData(9);
        foreach($revlists as $rev){
            if(empty($rev['carryover_id'])){
                return false;
            }
        }
        return true;
    }

    public static function getBalanceRateInfo(){
        $applist = ApplicationInfoRepository::getBalanceRateApplicationList();
        $bulist = ErpBusinessUnitRepository::getAreaList('',100);
        return ['applist'=>$applist,'bulist'=>$bulist];
    }

    public static function getBalanceRate($keyword){
        $brinfo = ApplicationBuRateEntity::find()->asArray()->all();
        $balancerate = $brinfo;
        $datareturn = [];
        foreach($balancerate as $key => $val){
            $balancerate[$key]['appname'] = ApplicationInfoRepository::getAppNameByBalanceRateId($balancerate[$key]['balance_rate_id']);
            $balancerate[$key]['BUName'] = ErpBusinessUnitRepository::getBunameByBuguid($balancerate[$key]['BUGUID']);
            $balancerate[$key]['editMode'] = '0';
            if(!empty($keyword)) {
                if (strpos($balancerate[$key]['appname'], $keyword) !== false || strpos($balancerate[$key]['BUName'], $keyword) !== false) {
                    $datareturn[] = $balancerate[$key];
                }
            }
        }
        if(!empty($keyword)){
            return $datareturn;
        }else {
            return $balancerate;
        }
    }
}